package com.junxonline.plat.dao.repository;

import com.junxonline.plat.dao.model.Household;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: HouseholdRepository
 * @Description: 住户数据操作类
 * @author JunX
 */

@Repository
public interface HouseholdRepository extends JpaRepository<Household, Long>, JpaSpecificationExecutor<Household>  {

    Household findByHouseholdId(Long householdId);

    List<Household> findByHouseholdIdIn(Long[] householdIds);

    @Modifying
    @Query(value ="DELETE FROM plat_household_position WHERE household_id = ?1 AND position_id = ?2", nativeQuery = true)
    void deleteHouseholdPositionIn(Long householdId, Long positionId);

    @Modifying
    @Query(value ="DELETE FROM plat_household_house WHERE household_id = ?1 AND house_id = ?2", nativeQuery = true)
    void deleteHouseholdHouseIn(Long householdId, Long positionId);

    @Query(value = "SELECT household.* FROM plat_household household ORDER BY household.household_id DESC LIMIT 1", nativeQuery = true)
    Household findByHouseholdIdLast();

    @Query(value = "SELECT household.* FROM plat_household household ORDER BY household.household_NO DESC LIMIT 1", nativeQuery = true)
    Household findByHouseholdNoLast();

    @Query(value = "SELECT household_position.* FROM plat_household_position household_position WHERE household_position.household_id = ?1", nativeQuery = true)
    List<Map<String, BigInteger>> findHousePositionByHouseholdId(Long householdId);

    @Modifying
    @Query(value = "INSERT INTO plat_household_position (household_id, position_id) VALUES(?1, ?2)", nativeQuery = true)
    void saveHousePositionIn(Long householdId, Long positionId);

    List<Household> findByHouseholdNameLikeAndIsHousehold(String householdName, Integer flag);

    List<Household> findByHouseholdNameLikeAndIsWorker(String householdName, Integer flag);
}
